#ifndef DISPATCH_OBJECT_P_H
#define DISPATCH_OBJECT_P_H

#include "dispatch_process.h"


namespace dispatch {
class ObjectPrivate : public Object
{
public:
    ObjectPrivate()
        : m_runFlag(false)
    {}
    ~ObjectPrivate()
    {
        if (m_runFlag)
        {
            m_dispath.run(FUNC(&ObjectPrivate::onInterrupt), this);
            m_dispath.interrupt();
        }
    }

    // 事件循环
    void execLoop()
    {
        m_runFlag = true;
        while (m_runFlag)
        {
            m_dispath.queueProcess();
        }
    }

public:
    dispatch::Queue m_dispath;
    bool            m_runFlag;

private:
    // 中断事件
    void onInterrupt()
    {
        m_runFlag = false;
    }
};

class LoopPrivate : public Object, public Loop
{
public:
    LoopPrivate()
    {
        this->setQueue(&m_dispath);
    }
    ~LoopPrivate() {}

    void postEvent(int sig, long w=0, long l=0, void *p=nullptr)
    {
        post(Loop::Evt(sig, w, l, p));
    }
    void execLoop()
    {
        this->exec();
    }

public:
    dispatch::Queue     m_dispath;
};
}


#endif // DISPATCH_OBJECT_P_H
